import router from "@/router/index";
import common from "@/utils/common.js";
import { getAdminToken, getRouterList } from "@/utils/auth";
import Router from "vue-router";
import { isNull } from "@/utils/service";

export default {
  init() {
    // 找到父路由
    const parentRoute = router.options.routes.find(route => route.path === "/index.vue");
    let routerList = getRouterList().filter(menu => !isNull(menu) && menu.parentId !== -1);

    routerList.forEach(item => {
      parentRoute.children.push({
        path: item.requestUrl.trim(),
        component: this.loadView(item.component.trim()),
        meta: {
          title: item.menuName,
          requireAuth: true,
          keepAlive: item.keepAlive
        }
      });
    });
    router.addRoutes([
      {
        path: "/err_404",
        component: this.loadView("error/404"),
        meta: {
          title: "找不到页面"
        }
      }
    ]);
    router.matcher = new Router(router.options).matcher;
  },

  // loadView(view) {  // 路由懒加载
  //   return resolve => {
  //     require.ensure([], (require) => {
  //       resolve(require("@/views/" + view + ".vue"));
  //     });
  //   };
  // }
  loadView (view) { // 路由懒加载
    return (resolve) => require([`@/views/${view}`], resolve)
  }

};
